package com.wn.service.impl;

import com.wn.dao.UserDao;
import com.wn.entity.ResponseEntity;
import com.wn.entity.User;
import com.wn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Override
    public ResponseEntity addOne(User user) {
        User user_=userDao.findByName(user.getUsername());
        if(user_!=null){
            return new ResponseEntity(500,"失败","用户名已存在");
        }else{
            if(user.getUsername()==null||user.getUsername()==""){
                return new ResponseEntity(500,"失败","用户名不能为空");
            }
            if(user.getPassword()==null||user.getPassword()==""){
                return new ResponseEntity(500,"失败","密码不能为空");
            }
            if(user.getEmail()==null||user.getEmail()==""){
                return new ResponseEntity(500,"失败","邮箱不能为空");
            }
            int j=userDao.addOne(user);
            if(j>0){
                return new ResponseEntity(200,"成功","注册成功");
            }
        }

        return null;
    }

    @Override
    public ResponseEntity login(User user) {
        if(user.getUsername()==null||user.getUsername()==""){
            return new ResponseEntity(500,"失败","用户名不能为空");
        }
        if(user.getPassword()==null||user.getPassword()==""){
            return new ResponseEntity(500,"失败","密码不能为空");
        }
        User user_ = userDao.findByName(user.getUsername());
        if(user_==null){
            return new ResponseEntity(500,"失败","用户名不存在");
        }
        if(user_.getPassword().equals(user.getPassword())){
            return ResponseEntity.SUCCESS;
        }else {
            return new ResponseEntity(500,"失败","密码错误");

        }
    }
}
